package com.cassiopae.services;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cassiopae.dao.ProfileDaoImpl;
import com.cassiopae.dao.UserDaoImpl;
import com.cassiopae.entity.Profile;
import com.cassiopae.entity.User;
import com.cassiopae.task.DumpTask;
import com.mysql.jdbc.StringUtils;

/**
 * Servlet implementation class DumpTaskServlet
 */
@WebServlet("/DumpTaskServlet")
@MultipartConfig
public class DumpTaskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ProfileDaoImpl profileDaoImpl = new ProfileDaoImpl();
	private UserDaoImpl userDaoImpl = new UserDaoImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DumpTaskServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String schemaSource = request.getParameter("Source_schema");
		int id = Integer.parseInt( request.getParameter("ProfileID"));
		String fileName = request.getParameter("FileDump");
		Profile profile = profileDaoImpl.retrieveProfile(id);
		String dumpType = request.getParameter("importDmp");
		
		
		profile.setFileName(fileName);
		profile.setSchemaSource(schemaSource);
		profile.setDumpType(dumpType);

		User user = (User) request.getSession().getAttribute("user");
		
		
		if ( request.getParameter("environmentDmp") != null ){
			
			Boolean isOtherProfileSelected =new Boolean( request.getParameter("environmentDmp") );
			if(isOtherProfileSelected){
				id = Integer.parseInt( request.getParameter("impSchemaSelected"));
				Profile otherProfile = profileDaoImpl.retrieveProfile(id);
				String otherSourceSchemaInput = request.getParameter("OtherSourceSchemaInput");
				if( isDifferentProfile( otherProfile, profile ) && !StringUtils.isNullOrEmpty(otherSourceSchemaInput) ){
					DumpTask dumpTask = new DumpTask(fileName, schemaSource, profile, dumpType,user );
					if("BACK".equalsIgnoreCase(profile.getImpSchema())){
						dumpTask.setOtherProfile(otherProfile);
						dumpTask.setOtherSourceSchemaInput(otherSourceSchemaInput);
					}else{
						dumpTask.setProfile(otherProfile);
						dumpTask.setSchemaSource(otherSourceSchemaInput);
						dumpTask.setOtherProfile(profile);
						dumpTask.setOtherSourceSchemaInput(schemaSource);
					}
					profile.setStatus(true);
					profileDaoImpl.updateStatusAndBeginTime(profile);
					otherProfile.setStatus(true);
					profileDaoImpl.updateStatusAndBeginTime(otherProfile);
					dumpTask.start();
					userDaoImpl.retrieveAllProfiles(user);
				}else{
					PrintWriter out = response.getWriter();
					response.setContentType("text/html");
					out.println("<html><body><script type=\"text/javascript\">alert('test');</script></body></html>");
				}
				
			}
		}else {
			DumpTask dumpTask = new DumpTask(fileName, schemaSource, profile, dumpType,user );
			dumpTask.start();
			
			profile.setStatus(true);
			profileDaoImpl.updateStatusAndBeginTime(profile);
			userDaoImpl.retrieveAllProfiles(user);
			
		}
		
//		try {
//			new MailTask(profile).sendInOutMessage(MailTask.MESSAGE_SUBJECT_OUT, "noelle.deungoue@cassiopae.com");
//		} catch (AddressException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (MessagingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		request.getSession().setAttribute("LISTPROFIL", user.getProfiles());
		getServletContext().getRequestDispatcher("/listProfile.jsp").forward(request, response);
	}

	private boolean isDifferentProfile(Profile otherProfile, Profile profile) {
		
		return otherProfile.getId() != profile.getId() && !(otherProfile.getImpSchema().equals(profile.getImpSchema()));
	}

}
